package com.example.fw;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


public class GroupHelper extends HelperWithWebDriverBase {
	
	private Set<GroupObject> groupCache;

	public GroupHelper(ApplicationManager manager) {
		super(manager);
	}

	public Set<GroupObject> getGroups() {
		if (groupCache == null) {
			buildGroupCache();
		}
		return groupCache;
	}
	
	private void buildGroupCache() {
		groupCache = new HashSet<GroupObject>();
		manager.getNavigationHelper().goToGroupListPage();
		WebElement form = findForm().get(1);
		List<WebElement> checkboxes = form.findElements(By.name(manager.getProperty("checkboxId")));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute(manager.getProperty("checkboxTitle"));
			Pattern p = Pattern.compile(manager.getProperty("groupPattern"));
			Matcher m = p.matcher(title);
			if (m.matches()) {
				title = m.group(1);
			}
			GroupObject group = new GroupObject()
				.setName(title)
				.setId(checkbox.getAttribute(manager.getProperty("checkboxValue")));
			groupCache.add(group);
		} 
		return;
	}

	private List<WebElement> findForm() {
		return findElements(By.tagName(manager.getProperty("groupsFormId")));
	}

	public void createGroup(GroupObject groupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupCreation();
		fillGroupForm(groupObject);
		submitGroupCreation();
		groupCache = null;
	}

	public void deleteGroup(int i) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupDeletion(i);
		groupCache = null;
	}

	public void modifyGroup(int i, GroupObject groupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupModification(i);
		fillGroupForm(groupObject);
		submitGroupModification();
		groupCache = null;
	}

	private void initGroupCreation() {
		click(By.name(manager.getProperty("addGroupsButton")));
	}

	private void submitGroupCreation() {
		click(By.name(manager.getProperty("sumbitButton")));
	}

	private void initGroupModification(int i) {
		checkGroup(i);
		click(By.name(manager.getProperty("editGroupsButton")));
	}

	private void submitGroupModification() {
		click(By.name(manager.getProperty("updateGroupsButton")));	
	}

	private void initGroupDeletion(int i) {
		checkGroup(i);
		click(By.name(manager.getProperty("deleteGroupsButton")));
	}

	private void checkGroup(int i) {
		click(By.xpath(manager.getProperty("groupCheckboxNumber")));
	}

	private void fillGroupForm(GroupObject groupObject) {
		type("group_name", groupObject.name);
		type("group_header", groupObject.header);
		type("group_footer", groupObject.footer);
	}

	public GroupObject getGroupWithNumber(int i) {
		manager.getNavigationHelper().goToGroupListPage();
		String title = findGroupWithNumber(i).getAttribute(manager.getProperty("checkboxTitle"));
		Pattern p = Pattern.compile(manager.getProperty("groupPattern"));
		Matcher m = p.matcher(title);
		if (m.matches()) {
			title = m.group(1);
		}
		GroupObject group = new GroupObject()
			.setName(title)
			.setId(findGroupWithNumber(i).getAttribute(manager.getProperty("checkboxValue")));
		return group;
	}

	private WebElement findGroupWithNumber(int i) {
		return findElement(By.xpath(manager.getProperty("groupCheckboxNumber")));
	}
}